package com.shirley.pers.zhufengfmvi.fragments;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shirley.pers.zhufengfmvi.R;
import com.shirley.pers.zhufengfmvi.adapters.CommonPagerAdapter;
import com.shirley.pers.zhufengfmvi.fragments.discovery.DiscoveryAnchorFragment;
import com.shirley.pers.zhufengfmvi.fragments.discovery.DiscoveryCategoryFragment;
import com.shirley.pers.zhufengfmvi.fragments.discovery.DiscoveryRadioFragment;
import com.shirley.pers.zhufengfmvi.fragments.discovery.DiscoveryRatingFragment;
import com.shirley.pers.zhufengfmvi.fragments.discovery.DiscoveryRecommandFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoveryFragment extends Fragment implements TabLayout.OnTabSelectedListener {


    private TabLayout mTabLayout;

    public DiscoveryFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.discovery_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTabLayout = ((TabLayout) view.findViewById(R.id.discovery_tab_bar));
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        mTabLayout.setOnTabSelectedListener(this);

        // TODO:  TabLayout与ViewPager联动
        ViewPager viewPager = ((ViewPager) view.findViewById(R.id.discovery_viewpager));
        List<BaseFragment> fragments = new ArrayList<>();
        fragments.add(new DiscoveryRecommandFragment());
        fragments.add(new DiscoveryCategoryFragment());
        fragments.add(new DiscoveryRadioFragment());
        fragments.add(new DiscoveryRatingFragment());
        fragments.add(new DiscoveryAnchorFragment());
        CommonPagerAdapter commonPagerAdapter = new CommonPagerAdapter(getChildFragmentManager(),fragments);
        viewPager.setAdapter(commonPagerAdapter);
        mTabLayout.setupWithViewPager(viewPager);
    }

    /**
     * Called when a tab enters the selected state.
     *
     * @param tab The tab that was selected
     */
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
    }

    /**
     * Called when a tab exits the selected state.
     *
     * @param tab The tab that was unselected
     */
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    /**
     * Called when a tab that is already selected is chosen again by the user. Some applications
     * may use this action to return to the top level of a category.
     *
     * @param tab The tab that was reselected.
     */
    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
